<template>
  <div class="login-container">
    <div class="loginBox">
      <el-form
        ref="formRef"
        :model="form"
        class="login-form"
        :rules="rules"
        label-width="auto"
        @keyup.enter="handleLogin"
      >
        <div class="title-container">
          <div class="logoInfo clearfix">
            <em class="logo"></em>
            <span>用户登录</span>
          </div>
        </div>
        <el-form-item prop="username">
          <el-input
            :prefix-icon="UserFilled"
            v-model="form.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :prefix-icon="Lock"
            v-model="form.password"
            placeholder="请输入密码"
            type="password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="captcha">
          <el-input
            :prefix-icon="Lock"
            v-model="form.captcha"
            placeholder="请输入验证码"
            style="width: 60%"
          ></el-input>
          <div
            @click="onCaptcha"
            style="cursor: pointer; margin-left: 10px; width: 25%; height: 45px"
          >
            <img
              alt="验证码"
              :src="captcha"
              style="width: 100%; height: 100%"
            />
          </div>
        </el-form-item>
        <el-button
          :loading="loading"
          type="primary"
          class="login-button"
          @click="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useStore } from 'vuex'
import { UserFilled, Lock } from '@element-plus/icons-vue'
import { Captcha } from '@/api/login'
const store = useStore()
const loading = ref(false)
const form = ref({
  username: 'admin',
  password: '1',
  captcha: '',
  IMGTEXT: ''
})
const captcha = ref('')

const rules = ref({
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur'
    }
  ],
  captcha: [
    {
      required: true,
      message: '请输入验证码',
      trigger: 'blur'
    }
  ]
})

const getList = () => {
  // 验证码接口
  Captcha().then((res) => {
    const r = 'data:image/png;base64,' + res.data.imgBase64
    captcha.value = r
    form.value.IMGTEXT = res.data.csrfToken
  })
}
const onCaptcha = () => {
  getList()
}
const formRef = ref(null)
const handleLogin = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      store.dispatch('app/Login', form.value).then((res) => {
        if (res.message === '失败') {
          loading.value = false
        }
        if (res.data.fail === '验证码错误') {
          getList()
          loading.value = false
        }
      })
    } else {
      loading.value = false
      console.log('error submit!!')
      return false
    }
  })
}
getList()
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #333;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 88%;
    input {
      // background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-button--medium {
    height: 50px;
    line-height: 20px;
    font-size: 22px;
  }
  .el-input__wrapper {
    width: 100%;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  background: url('../../assets/bg.jpg');
  -moz-background-size: 100% 100%;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  .loginBox {
    height: 100%;
    width: 100%;
    background: url('../../assets/logingBg.png') no-repeat 100% 100%;
    position: relative;
  }
  .login-form {
    position: absolute;
    left: 20%;
    top: 50%;
    width: 500px;
    padding: 35px 35px 15px 35px;
    margin: -200px 0 0 0;
    background: #f5f5f5;
  }
  .tips {
    font-size: 16px;
    line-height: 28px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }

  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      // font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

.logoInfo span {
  font-size: 22px;
  padding: 0 10px;
  float: left;
}
.logoInfo .logo {
  display: inline-block;
  width: 35px;
  height: 26px;
  margin-bottom: 20px;
}
.login-button {
  width: 88%;
  height: 50px;
  margin-bottom: 20px;
  font-size: 22px;
}
</style>
